package com.example.tao_city.text1;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import org.w3c.dom.Text;

/**
 * Created by TAO_CITY on 2018/1/8.
 */

public class Transactionpassword extends AppCompatActivity implements View.OnClickListener{
    private TextView text1,text2;
    private LinearLayout layout1,layout2;
    private ImageView mode1,mode2;
    private ImageView back;
    private Button con1,con2,put;
    private EditText oldpw,newpw,pwagin;




    @Override
    protected void onCreate(Bundle savep){
        super.onCreate(savep);
        setContentView(R.layout.transactionp);
        initbutton();
    }

    private void initbutton(){
        text2=findViewById(R.id.transactionp_text2);
        text1=findViewById(R.id.transactionp_text1);
        layout1=findViewById(R.id.transactionp_layout1);
        layout2=findViewById(R.id.transactionp_layout2);
        mode1=findViewById(R.id.transactionp_mode1);
        mode2=findViewById(R.id.transactionp_mode2);
        back=findViewById(R.id.transaction_back);
        con1=findViewById(R.id.transactionp_confirm);
        con2=findViewById(R.id.transactionp_confirm1);
        oldpw=findViewById(R.id.transactionp_oldpassword);
        newpw=findViewById(R.id.transanctionp_newpassword);
        pwagin=findViewById(R.id.transactionp_passwordagin);
        put=findViewById(R.id.transactionp_put);

        con1.setOnClickListener(this);
        con2.setOnClickListener(this);
        back.setOnClickListener(this);
        text1.setOnClickListener(this);
        text2.setOnClickListener(this);

    }
    @SuppressLint("ResourceAsColor")
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.transactionp_text2:
                layout1.setVisibility(View.INVISIBLE);
                layout2.setVisibility(View.VISIBLE);
                text1.setTextColor(Color.parseColor("#808080"));
                mode1.setBackgroundColor(Color.parseColor("#e8e8e8"));
                text2.setTextColor(Color.parseColor("#458fee"));
                mode2.setBackgroundColor(Color.parseColor("#458fee"));


                break;
            case R.id.transactionp_text1:
                layout1.setVisibility(View.VISIBLE);
                layout2.setVisibility(View.INVISIBLE);
                text2.setTextColor(Color.parseColor("#808080"));
                mode2.setBackgroundColor(Color.parseColor("#e8e8e8"));
                text1.setTextColor(Color.parseColor("#458fee"));
                mode1.setBackgroundColor(Color.parseColor("#458fee"));
                break;
            case R.id.transaction_back:
                finish();
                break;
            case R.id.transactionp_confirm:
                String op = String.valueOf(oldpw.getText());
                String np = String.valueOf(newpw.getText());
                String pa = String.valueOf(pwagin.getText());
                if (op.equals("123456") && np.equals(pa) && !(np.equals(""))) {
                    MyToast.makeText(this, "密码修改成功", Toast.LENGTH_SHORT).show();
                    screenchange();
                    Runnable ra = new Runnable() {
                        @Override
                        public void run() {
                            finish();
                        }
                    };
                    Handler ahander = new Handler();
                    ahander.postDelayed(ra, 1000);
                } else if (!(op.equals("123456"))) {
                    MyToastf.makeText(this, "原密码错误", Toast.LENGTH_SHORT).show();
                    screenchange();
                } else if (!(np.equals(pa))) {
                    MyToastf.makeText(this, "新密码不一致", Toast.LENGTH_SHORT).show();
                    screenchange();
                }
                break;
            case R.id.transactionp_confirm1:
                Toast.makeText(this,"手机模式OK",Toast.LENGTH_SHORT).show();
                break;
        }

    }
    private void screenchange() {
        Handler mhander = new Handler();//新建一个Hander
        mhander.postDelayed(ma, 2000);//两秒后执行ma线程
        //下面4行代码是屏幕变暗
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.alpha = 0.6f;
        getWindow().setAttributes(lp);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        oldpw.setEnabled(false);
        newpw.setEnabled(false);
        pwagin.setEnabled(false);
        back.setClickable(false);
        text1.setClickable(false);
        text2.setClickable(false);
        put.setClickable(false);

    }

    private Runnable ma = new Runnable() {
        @Override
        public void run() {
            //下面4行代码使屏幕变回亮的样式
            WindowManager.LayoutParams lp = getWindow().getAttributes();
            lp.alpha = 1.0f;
            getWindow().setAttributes(lp);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
            oldpw.setEnabled(true);
            newpw.setEnabled(true);
            pwagin.setEnabled(true);
            back.setClickable(true);
            text1.setClickable(true);
            text2.setClickable(true);
            put.setClickable(true);

        }
    };
}
